//
//  MainTabbarVC.m
//  tristana
//
//  Created by 李康 on 2021/1/5.
//  Copyright © 2021 李康. All rights reserved.
//

#import "MainTabbarVC.h"
#import "TTabbar.h"
#import "MessageViewController.h"
#import "RecodeViewController.h"
#import "MineViewController.h"
#import "HomeViewController.h"
#import "UserService.h"
#import "TSLoginVC.h"

@interface MainTabbarVC ()<UITabBarControllerDelegate>
@property (nonatomic, strong) TTabbar *mTabbar;


@end

@implementation MainTabbarVC

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupUI];
}

- (void)setupUI {
    self.delegate = self;
    [self addChildVc:[HomeViewController new] title:@"首页" image:@"tab_home_ns" selectedImage:@"tab_home_s"];
    [self addChildVc:[MessageViewController new] title:@"知识库" image:@"tab_zhishi_ns" selectedImage:@"tab_zhishi_s"];
    [self addChildVc:[RecodeViewController new] title:@"记录" image:@"tab_jilu_ns" selectedImage:@"tab_jilu_s"];
    [self addChildVc:[MineViewController new] title:@"个人中心" image:@"tab_geren_ns" selectedImage:@"tab_geren_s"];

//    [self addTabbar];
}

- (void)addTabbar {
    _mTabbar = [[TTabbar alloc] init];
    
    [_mTabbar.centerBtn addTarget:self action:@selector(buttonAction) forControlEvents:UIControlEventTouchUpInside];
    
    //选中时的颜色
    
    _mTabbar.tintColor = [UIColor colorWithRed:19.0/255.0 green:34.0/255.0 blue:122/255.0 alpha:1];
    
    //透明设置为NO，显示白色，view的高度到tabbar顶部截止，YES的话到底部
    _mTabbar.translucent = NO;
    [self setValue:_mTabbar forKeyPath:@"tabBar"];
}
#pragma mark - Delegate

#pragma mark - Private Method
- (void)addChildVc:(UIViewController *)childVc title:(NSString *)title image:(NSString *)image selectedImage:(NSString *)selectedImage
{
    // 设置子控制器的文字(可以设置tabBar和navigationBar的文字)
    childVc.title = title;
    
    // 设置子控制器的tabBarItem图片
    childVc.tabBarItem.image = [UIImage imageNamed:image];
    // 禁用图片渲染
    childVc.tabBarItem.selectedImage = [[UIImage imageNamed:selectedImage] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    
    // 设置文字的样式
//    [childVc.tabBarItem setTitleTextAttributes:@{NSForegroundColorAttributeName : [UIColor blueColor]} forState:UIControlStateNormal];
//    [childVc.tabBarItem setTitleTextAttributes:@{NSForegroundColorAttributeName : [UIColor redColor]} forState:UIControlStateSelected];
    
    if (@available(iOS 13.0, *)) {
      UITabBarItemAppearance *inlineLayoutAppearance = [[UITabBarItemAppearance  alloc] init];
//      inlineLayoutAppearance.normal.titlePositionAdjustment = self.titlePositionAdjustment;
        [inlineLayoutAppearance.normal setTitleTextAttributes:@{NSForegroundColorAttributeName : [UIColor colorWithRed:138.0/255.0 green:138.0/255.0 blue:138.0/255.0 alpha:1]}];
      [inlineLayoutAppearance.selected setTitleTextAttributes:@{NSForegroundColorAttributeName : [UIColor colorWithRed:19.0/255.0 green:34.0/255.0 blue:122/255.0 alpha:1]}];

      UITabBarAppearance *standardAppearance = [[UITabBarAppearance alloc] init];
      standardAppearance.stackedLayoutAppearance = inlineLayoutAppearance;
//      standardAppearance.backgroundColor = [UIColor orangeColor];
//      standardAppearance.shadowImage = [UIImage qmui_imageWithColor:UIColorHex(E5E5E5) size:CGSizeMake(SCREEN_WIDTH, PixelOne) cornerRadius:0];
      self.tabBar.standardAppearance = standardAppearance;
          [[UITabBar appearance] setUnselectedItemTintColor:[UIColor colorWithRed:19.0/255.0 green:34.0/255.0 blue:122/255.0 alpha:1]]; //

    }else {

        [[UITabBarItem appearance] setTitleTextAttributes:@{NSForegroundColorAttributeName : [UIColor colorWithRed:138.0/255.0 green:138.0/255.0 blue:138.0/255.0 alpha:1]} forState:UIControlStateNormal];
        [[UITabBarItem appearance] setTitleTextAttributes:@{NSForegroundColorAttributeName : [UIColor colorWithRed:19.0/255.0 green:34.0/255.0 blue:122/255.0 alpha:1]} forState:UIControlStateSelected];
    }
    
    // 为子控制器包装导航控制器
    UINavigationController *navigationVc = [[UINavigationController alloc] initWithRootViewController:childVc];
    // 添加子控制器
    [self addChildViewController:navigationVc];
    
    

}

//点击跳转 判断是否登陆
- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController{
    NSInteger index = [self.viewControllers indexOfObject:viewController];
    if (index == 2 && ![UserService isLogin]) {
        [TSLoginVC showLoginVC];
        return NO;
    }else{
        return YES;
    }
}


#pragma mark - Overwrite Method

#pragma mark - Event Response
- (void)buttonAction {
    self.selectedIndex = 1;
}
#pragma mark - Setter

#pragma mark - Getter


@end
